import api from './axios';

// 用户登录
export const login = async (username, password) => {
  try {
    const response = await api.post('/auth/login/', { username, password });
    const { token, user } = response.data;
    localStorage.setItem('token', token); // 保存 token 到本地存储
    return user;
  } catch (error) {
    console.error('登录失败:', error);
    throw error;
  }
};

// 获取学习记录
export const getStudyRecords = async () => {
  try {
    const response = await api.get('/study/records/');
    return response;
  } catch (error) {
    console.error('获取学习记录失败:', error);
    throw error;
  }
};

// 创建学习记录
export const createStudyRecord = async (studyData) => {
  try {
    const response = await api.post('/study/records/', studyData);
    return response;
  } catch (error) {
    console.error('创建学习记录失败:', error);
    throw error;
  }
};

// 获取待办事项
export const getTodos = async () => {
  try {
    const response = await api.get('/todo/items/');
    return response;
  } catch (error) {
    console.error('获取待办事项失败:', error);
    throw error;
  }
};

// 创建待办事项
export const createTodo = async (listId, todoData) => {
  try {
    const response = await api.post(`/todo/lists/${listId}/items/`, todoData);
    return response;
  } catch (error) {
    console.error('创建待办事项失败:', error);
    throw error;
  }
};

// 获取学习墙帖子
export const getWallPosts = async () => {
  try {
    const response = await api.get('/wall/posts/');
    return response;
  } catch (error) {
    console.error('获取学习墙帖子失败:', error);
    throw error;
  }
};

// 发布学习墙帖子
export const createWallPost = async (postData) => {
  try {
    const response = await api.post('/wall/posts/', postData);
    return response;
  } catch (error) {
    console.error('发布学习墙帖子失败:', error);
    throw error;
  }
}; 